<?php

class Admin_bannerController extends Admin_baseController {

	public function listAction() {
		$parent_id = intval(Request::getParams('parent_id'));
		if (!$parent_info = self::model('banner')->getOne('banner_id=?', $parent_id)) {
			Response::showMsg('广告信息获取失败');
		}
		$this->assign([
			'parent_info' => $parent_info,
			'jump_type_list' => self::model('banner')->jumpTypeList(),
			'list' => self::model('banner')->order('sort desc')->getAll(['parent_id=?'], [$parent_id]),
		]);
		$this->display();
	}

	public function editAction() {
		$parent_id = intval(Request::getParams('parent_id', 0));
		$banner_id = Request::getParams('banner_id');
		$data = [];
		$data['name'] = Request::getParams('name', '');
		$data['image'] = Request::getParams('image', '');
		$data['jump_type'] = Request::getParams('jump_type', '');
		$data['jump_id'] = intval(Request::getParams('jump_id', 0));
		$data['jump_url'] = Request::getParams('jump_url', '');
		$data['parent_id'] = intval(Request::getParams('parent_id', 0));
		$data['status'] = intval(Request::getParams('status', 1));
		$data['sort'] = intval(Request::getParams('sort', 0));
		$data['update_time'] = date('Y-m-d H:i:s');

		$banner_info = [];
//        if(!$parent_info = self::model('banner')->findById($parent_id)) {
		//            Response::showMsg('所属广告类别查询失败');
		//        }
		if ($banner_id && !$banner_info = self::model('banner')->getOne('banner_id=?', $banner_id)) {
			Response::showMsg('信息查询失败，请稍后重试');
		}
		$parent_id = isset($banner_info['parent_id']) ? $banner_info['parent_id'] : $parent_id;
		if (Request::isGet()) {
			$this->assign([
				'parent_id' => $parent_id,
				'jump_type_list' => self::model('banner')->jumpTypeList(),
				'banner_info' => $banner_info,
//                'parent_info' => $parent_info
			]);
			$this->display();
			exit;
		}
		if ($banner_id) {
//修改
			if (self::model('banner')->updateById($banner_id, $data)) {
				Response::ajax(200, '修改成功');
			}
		} else {
//新增
			$data['add_time'] = date('Y-m-d H:i:s');
			if (self::model('banner')->insert($data)) {
				Response::ajax(200, '添加成功');
			}
		}
		Response::ajax(NORMAL_ERROR, '数据处理失败，请稍后重试');
	}

	public function deleteAction() {
		$banner_id = intval(Request::getParams('banner_id'));
		if (self::model('banner')->delete('banner_id=?', $banner_id)) {
			Response::ajax(200, '删除成功');
		}
		Response::ajax(NORMAL_ERROR, '删除失败,请稍后再试');
	}

	public function getJumpIdListAction() {
		$jump_type = Request::getParams('jumpType');
		switch ($jump_type) {
		case 'webview':
			Response::ajax(NORMAL_ERROR, '请填写跳转地址');
		case 'goodsInfo':
			Response::ajax(200, 'success', self::model('goods')->fields(['goods_id as id, name'])->order('goods_id desc')->getAll());
		case 'dynamicInfo':
			Response::ajax(200, 'success', self::model('dynamic')->fields(['dynamic_id as id, title as name'])->getAll('status=1'));
		case 'goodsCategory':
			Response::ajax(200, 'success', self::model('goods_category')->getBannerSelect());
		case 'shopId':
			Response::ajax(200, 'success', self::model('shop')->fields(['shop_id as id, name'])->getAll('status=1'));
		case 'activityInfo':
			Response::ajax(200, 'success', self::model('tag')->fields(['id, title as name'])->getAll());
		default:
			Response::ajax(NORMAL_ERROR, '跳转方式选择有误');
		}
	}
}